#########################################################################################
####################################  FIGURE A4   #######################################
######## Tracing the source of disempowerment: Votes per man/woman, by ##################
###############  sources and across condition, separately by study  #####################
#########################################################################################


rm(list=ls())

# Import Study 1 analysis produced in STATA do file
library(dplyr)
require(ggplot2)
require(ggpubr)
require(gridExtra)

#setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_a4")

#########################################################################################


stataexport <- read.csv("figure_a4_input.csv", stringsAsFactors = FALSE)

stataexport <- stataexport %>%
  mutate(condition_maj_min = case_when(
    condition_maj_min=="Majority Men Condition" ~ "Majority Male",
    condition_maj_min=="Majority Women Condition" ~ "Majority Female",
    TRUE ~ "999"
  ))

stataexport$condition_maj_min <- factor(stataexport$condition_maj_min, levels = c("Majority Male", "Majority Female"))


# convert to votes per woman
stataexport$X_val2 <- stataexport$X_val/stataexport$X_exp
stataexport$X_exp2 <- 1
stataexport$ci2 <- stataexport$ci/stataexport$X_exp
stataexport <- stataexport %>% select(-c(X_val, X_exp, ci)) %>% dplyr::rename(X_val = X_val2, X_exp = X_exp2, ci = ci2)

colorW<-"#8856a7"
colorM<- "#9ebcda"

segx <- c(.6, 1.6,1.05, 2.05)
segxend <- c(.95, 1.95,1.40,  2.40)


p1df <- stataexport %>% filter(variable=="votedForWomanMInf" & study == 2)
p1df$`Source of votes` <- p1df$sex
p1 <-
  ggplot(p1df, aes(x=condition_maj_min, y=X_val, colour = `Source of votes`)) +
  expand_limits(y=c(0,1.5)) +
  geom_bar(position=position_dodge(), fill = c(colorM,  colorM,colorW, colorW), stat="identity",  size=.3) +
  theme(legend.position = c(0.3, 0.85))  + scale_color_manual(values = c(colorM, colorW)) + scale_fill_manual(breaks=c("Men", "Women")) +
  guides(color = guide_legend(override.aes = list(fill = c(colorM, colorW)))) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, group=`Source of votes`), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx, xend = segxend, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("All votes") +
  ylab("'most influential' votes per woman") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_text(size=12),
        legend.text=element_text(size=10))

p1

p2df <- stataexport %>% filter(variable=="selfvote" & sex=="Women" & study==2)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)

p2 <- ggplot(p2df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,1.5)) +
  geom_bar(position=position_dodge(), fill = c(colorW, colorW), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Women's votes for self") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")


p3df <- stataexport %>% filter(variable=="otherFvote" & sex == "Women" & study==2)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)


p3 <- ggplot(p3df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,1.5)) +
  geom_bar(position=position_dodge(), fill = c(colorW, colorW), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Women's votes for other women") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")

stack <- grid.arrange(p1,p2,p3, nrow = 1)


########################################

p1df <- stataexport %>% filter(variable=="votedForManMInf" & study==2)
segx <- c(.6, 1.6,1.05, 2.05)
segxend <- c(.95, 1.95,1.40,  2.40)

p1 <- ggplot(p1df, aes(x=condition_maj_min, y=X_val, colour = sex)) +
  expand_limits(y=c(0,2)) +
  geom_bar( position=position_dodge(), fill = c(colorM,  colorM,colorW, colorW), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx, xend = segxend, y = X_exp, yend = X_exp, colour = "#999999"), size=1, linetype="dashed") +
  xlab("All votes") +
  ylab("'most influential' votes per man") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  scale_color_manual(name='TK',
                     breaks=c('TK1', 'TK2'),
                     values=c('Men'='dodgerblue3', 'Women'='darkgoldenrod2')) +
  theme(legend.title=element_text(size=20),
        legend.text=element_text(size=14))

p2df <- stataexport %>% filter(variable=="selfvote" & sex=="Men"  & study==2)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)

p2 <- ggplot(p2df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,2)) +
  geom_bar(position=position_dodge(), fill = c(colorM,  colorM), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Men's votes for self") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")


p3df <- stataexport %>% filter(variable=="otherMvote" & sex == "Men"  & study==2)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)


p3 <- ggplot(p3df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,2)) +
  geom_bar(position=position_dodge(), fill = c(colorM,  colorM), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Men's votes for other men") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")

stack2 <- grid.arrange(p1,p2,p3, nrow = 1)

finalgraph <- grid.arrange(stack, stack2, nrow=2)

ggsave(finalgraph, file = 'figure_a4b.pdf', width = 14, height = 10, units = "in")
# end study 2
#######################################

#######################################
# Study 1

segx <- c(.6, 1.6,1.05, 2.05)
segxend <- c(.95, 1.95,1.40,  2.40)


p1df <- stataexport %>% filter(variable=="votedForWomanMInf" & study == 1)
p1df$`Source of votes` <- p1df$sex
p1 <-
  ggplot(p1df, aes(x=condition_maj_min, y=X_val, colour = `Source of votes`)) +
  expand_limits(y=c(0,1.5)) +
  geom_bar(position=position_dodge(), fill = c(colorM,  colorM,colorW, colorW), stat="identity",  size=.3) +
  theme(legend.position = c(0.3, 0.85))  + scale_color_manual(values = c(colorM, colorW)) + scale_fill_manual(breaks=c("Men", "Women")) +
  guides(color = guide_legend(override.aes = list(fill = c(colorM, colorW)))) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, group=`Source of votes`), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx, xend = segxend, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("All votes") +
  ylab("'most influential' votes per woman") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_text(size=12),
        legend.text=element_text(size=10))

p1

p2df <- stataexport %>% filter(variable=="selfvote" & sex=="Women" & study==1)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)

p2 <- ggplot(p2df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,1.5)) +
  geom_bar(position=position_dodge(), fill = c(colorW, colorW), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Women's votes for self") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")


p3df <- stataexport %>% filter(variable=="otherFvote" & sex == "Women" & study==1)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)


p3 <- ggplot(p3df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,1.5)) +
  geom_bar(position=position_dodge(), fill = c(colorW, colorW), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Women's votes for other women") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")

stack <- grid.arrange(p1,p2,p3, nrow = 1)


########################################

p1df <- stataexport %>% filter(variable=="votedForManMInf" & study==1)
segx <- c(.6, 1.6,1.05, 2.05)
segxend <- c(.95, 1.95,1.40,  2.40)

p1 <- ggplot(p1df, aes(x=condition_maj_min, y=X_val, colour = sex)) +
  expand_limits(y=c(0,2)) +
  geom_bar( position=position_dodge(), fill = c(colorM,  colorM,colorW, colorW), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx, xend = segxend, y = X_exp, yend = X_exp, colour = "#999999"), size=1, linetype="dashed") +
  xlab("All votes") +
  ylab("'most influential' votes per man") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  scale_color_manual(name='TK',
                     breaks=c('TK1', 'TK2'),
                     values=c('Men'='dodgerblue3', 'Women'='darkgoldenrod2')) +
  theme(legend.title=element_text(size=20),
        legend.text=element_text(size=14))

p2df <- stataexport %>% filter(variable=="selfvote" & sex=="Men"  & study==1)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)

p2 <- ggplot(p2df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,2)) +
  geom_bar(position=position_dodge(), fill = c(colorM,  colorM), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Men's votes for self") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")


p3df <- stataexport %>% filter(variable=="otherMvote" & sex == "Men"  & study==1)
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)


p3 <- ggplot(p3df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,2)) +
  geom_bar(position=position_dodge(), fill = c(colorM,  colorM), stat="identity", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, colour=sex, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Men's votes for other men") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")

stack2 <- grid.arrange(p1,p2,p3, nrow = 1)

finalgraph <- grid.arrange(stack, stack2, nrow=2)

ggsave(finalgraph, file = 'figure_a4a.pdf', width = 14, height = 10, units = "in")

